package com.idas;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import com.idas.APIContext;
import com.idas.comp.UserAccess;

public class APIContext extends Thread {

	public static final String AUTH_USER_KEY = "auth_user";

	private Hashtable<String, Object> keysTable = new Hashtable<String, Object>();

	public APIContext(Runnable r) {
		super(r);
	}

	public void setkey(String key, Object obj) {
		// if(!keysTable.containsKey(key))
		keysTable.put(key, obj);
	}

	public Hashtable<String, Object> getObjects() {
		return keysTable;
	}

	public Object getObject(String key) {
		return keysTable.get(key);
	}

	public String getKey(UserAccess obj) {
		String key = null;
		if (keysTable.containsValue(obj)) {
			Set<String> keys = keysTable.keySet();
			Iterator<String> iter = keys.iterator();
			while (iter.hasNext()) {
				key = iter.next();
				Object value = keysTable.get(key);
				if (value.equals(obj)) {
					break;
				}
			}
		}
		return key;
	}

	public void clearContext() {
		keysTable.clear();
	}

	public static APIContext getCurrentContext() {
		return (APIContext) Thread.currentThread();
	}

}